package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.config.AppEmailConfig;
import com.dhcc.bpm.common.utils.EmailUtils;
import com.dhcc.bpm.common.utils.MessageUtils;
import com.dhcc.bpm.modules.content.entity.Message;
import com.dhcc.bpm.modules.content.entity.Suggestion;
import com.dhcc.bpm.modules.content.service.IMessageService;
import com.dhcc.bpm.modules.content.service.ISuggestionService;
import com.dhcc.bpm.modules.content.vo.SuggestionVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  控制器
 *
 * @author donghang
 * @since 2020-03-16
 */
@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("bpm-content/suggestion")
@Api(value = "", tags = "接口")
public class SuggestionController extends BladeController {
	@Autowired
	private EmailUtils emailUtils;
	@Autowired
	private AppEmailConfig appEmailConfig;
	@Autowired
	private ISuggestionService suggestionService;
	@Autowired
	private ISerialNumberService serialNumberService;
	@Autowired
	private IMessageService messageService;
	@Value("${bpm.approval.isSendMail}")
	private boolean isSendFlag;
	/**
	 * 详情
	 */
	@ApiLog("建议模块表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入suggestion")
	public R<Suggestion> detail(SuggestionVO suggestion) {
		System.out.println(suggestion);
		System.out.println(suggestion.getId());
//		Suggestion detail = suggestionService.getOne(Condition.getQueryWrapper(suggestion));
		Suggestion detail = suggestionService.selectDetailVO(suggestion);
		return R.data(detail);
	}

	/**
	 * 分页 
	 */
	@ApiLog("建议模块表列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入suggestion")
	public R<IPage<SuggestionVO>> list(SuggestionVO suggestion, Query query) {
		IPage<SuggestionVO> pages = suggestionService.selectSuggestionPageVO(Condition.getPage(query), suggestion);
		return R.data(pages);
	}

	/**
	 * 自定义分页 
	 */
	@ApiLog("建议模块表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入suggestion")
	public R<IPage<SuggestionVO>> page(SuggestionVO suggestion, Query query) {
		IPage<SuggestionVO> pages = suggestionService.selectSuggestionPage(Condition.getPage(query), suggestion);
		return R.data(pages);
	}

	/**
	 * 新增 
	 */
	@ApiLog("建议模块表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入suggestion")
	public R save(@Valid @RequestBody Suggestion suggestion) {
		return R.status(suggestionService.save(suggestion));
	}

	/**
	 * 修改 
	 */
	@ApiLog("建议模块表更新")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入suggestion")
	public R update(@Valid @RequestBody Suggestion suggestion) {
		return R.status(suggestionService.updateById(suggestion));
	}

	/**
	 * 新增或修改 
	 */
	@ApiLog("建议模块表提交")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入suggestion")
	@Transactional(rollbackFor = Exception.class)
	public R submit(@Valid @RequestBody Suggestion suggestion) throws ParseException {
		suggestion.setReplyFlag(2);
		BladeUser bladeUser = AuthUtil.getUser();
		suggestion.setReplyUser(bladeUser.getUserId());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		suggestion.setReplyTime(df.parse(df.format(new Date())));
		boolean bool = suggestionService.saveOrUpdate(suggestion);
		suggestion = suggestionService.getById(suggestion.getId());
		if(bool){
			if(suggestion.getOpcUserId()!=0){
				//发送推送回复消息
				Message message = new Message();
				String msgCode = serialNumberService.generateSerialNumberByModelCode("opc_access_control");
				suggestion = suggestionService.getById(suggestion.getId());
				message.setMsgCode(msgCode);
				message.setMsgType("1");
				message.setReceiver(suggestion.getOpcUserId().toString());
				message.setMsgName("回复" + suggestion.getTitle());
				message.setMsgContent(suggestion.getReplyContent());
				message.setDescribe("回复" + suggestion.getTitle() + "消息");
				message.setPushStatus(2);
				bool = messageService.saveOrUpdate(message);
				if(bool){
					MessageUtils utils = new MessageUtils();
					utils.sendMessage("开放平台",message.getId().toString());
				}
			}

			if(isSendFlag){
				//发送邮件通知用户，判断用户是否匿名
				if(suggestion.getOpcUserId()!=0){
					String mailTitle = appEmailConfig.getTitle() + "门户建议回复通知";
					String msg = "尊敬的用户，您好！\r\n" + "您提的意见：" + suggestion.getTitle() + " 已被回复，请登录开放门户网站查看具体回复内容！";
					emailUtils.sendEmail( mailTitle, suggestion.getMail(), mailTitle, msg);
				}else {
					String mailTitle = appEmailConfig.getTitle() + "门户建议回复通知";
					String msg = "尊敬的用户，您好！\r\n" + "您提的意见：" + suggestion.getTitle() + " 已回复，内容如下 " + suggestion.getReplyContent();
					emailUtils.sendEmail( mailTitle, suggestion.getMail(), mailTitle, msg);
				}
			}

		}
		return R.status(bool);
	}

	
	/**
	 * 删除 
	 */
	@ApiLog("建议模块表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(suggestionService.removeByIds(Func.toLongList(ids)));
	}

	
}
